package com.adopapa.qqapi.application.service;

import java.io.IOException;
import java.text.MessageFormat;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;

import com.adopapa.qqapi.domain.AccessToken;
import com.adopapa.qqapi.domain.OAuthUserInfo;
import com.adopapa.qqapi.domain.OpenId;
import com.adopapa.qqapi.utils.ApiUrls;
import com.adopapa.qqapi.utils.JsonUtil;
import com.adopapa.qqapi.utils.QQUtil;



public class QQApi {
	
	private final static Logger LOGGER = Logger.getLogger(QQApi.class);
	
	public static String getOAuth2Url4Pc(String responseType, String appId, String redirectUrl,  String scope, String state) {
		return MessageFormat.format(ApiUrls.PC_AUTHORIZATION_CODE_URL, responseType, appId, redirectUrl, scope, state);
	}
	
	/**
	 * 实际上调用的是pc的接口，只是传入了display=mobile参数
	 */
	public static String getOAuth2Url4Wap(String responseType, String appId, String redirectUrl,  String scope, String state) {
		return MessageFormat.format(ApiUrls.WAP_AUTHORIZATION_CODE_URL, responseType, appId, redirectUrl,  scope, state);
	}
	
	
//	public static AccessToken getAccessToken(HttpServletRequest request) {
//		String token = request.getParameter("access_token");
//		String expires = request.getParameter("expires_in");
//		if (expires == null || token == null) {
//			AccessToken accessToken = new AccessToken();
//			accessToken.setAccessToken(token);
//			accessToken.setExpiresIn(Long.valueOf(expires));
//			return accessToken;
//		}
//		return null;
//	}
	
	//callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} );
	public static OpenId getPcOpenId(String accessToken) throws Exception {
		String url = MessageFormat.format(ApiUrls.PC_GET_OPENID_URL, accessToken);
		String result = QQUtil.httpsRequest(url, "GET", null);
		if (result == null || result.indexOf("callback") < 0) {
			return null;
		}
		int first = result.indexOf("{");
		int last = result.lastIndexOf("}");
		result = result.substring(first, last + 1);
		OpenId openId = JsonUtil.json2Bean(result, OpenId.class);
		return openId;
	}
	
	//client_id=100222222&openid=1704************************878C
	@Deprecated
	public static OpenId getWapOpenId(String accessToken) throws Exception {
		String url = MessageFormat.format(ApiUrls.WAP_GET_OPENID_URL, accessToken);
		String result = QQUtil.httpsRequest(url, "GET", null);
		LOGGER.info(result);
		String[] parts = result.split("&");
		String appId = parts[0].substring(parts[0].indexOf("=") + 1);
		String openid = parts[1].substring(parts[1].lastIndexOf("=") + 1);
		OpenId openId = new OpenId();
		openId.setAppId(appId);
		openId.setOpenId(openid);
		return openId;
	}
	
	public static AccessToken getAccessToken4Pc(String grantType, String appId, String appSecret, String code, String redirectUrl) {
		String url = MessageFormat.format(ApiUrls.PC_ACCESS_TOKEN, grantType, appId, appSecret, code, redirectUrl);
		String result = QQUtil.httpsRequest(url, "GET", null);
		String[] parts = result.split("&");
		String accessToken 	= parts[0].substring(parts[0].indexOf("=") + 1);
		String expiresIn 	= parts[1].substring(parts[0].indexOf("=") + 1);
		String refreshToken	= parts[2].substring(parts[0].indexOf("=") + 1);		
		AccessToken token = new AccessToken();
		token.setAccessToken(accessToken);
		token.setExpiresIn(Long.valueOf(expiresIn));
		token.setRefreshToken(refreshToken);
		return  token;
	}
	
	@Deprecated
	public static AccessToken getAccessToken4Wap(String grantType, String appId, String appSecret, String code, String redirectUrl) {
		String url =  MessageFormat.format(ApiUrls.WAP_ACCESS_TOKEN, grantType, appId, appSecret, code, redirectUrl);
		String result = QQUtil.httpsRequest(url, "GET", null);
		String[] parts = result.split("&");
		String accessToken 	= parts[0].substring(parts[0].indexOf("=") + 1);
		String expiresIn 	= parts[1].substring(parts[0].indexOf("=") + 1);
		String refreshToken	= parts[2].substring(parts[0].indexOf("=") + 1);		
		AccessToken token = new AccessToken();
		token.setAccessToken(accessToken);
		token.setExpiresIn(Long.valueOf(expiresIn));
		token.setRefreshToken(refreshToken);
		return  token;
	}
	
	//https://graph.qq.com/user/get_user_info?access_token=*************&oauth_consumer_key=12345&openid=****************
	public static OAuthUserInfo getUserInfo(String accessToken, String appId, String openId) throws Exception {
		String url = MessageFormat.format(ApiUrls.GET_USER_INFO_URL, accessToken, appId, openId);
		String result = QQUtil.httpsRequest(url, "GET", null);
		OAuthUserInfo userInfo = JsonUtil.json2Bean(result, OAuthUserInfo.class);
		return userInfo;
		
	}
	
	
//	public static String getAccessToken4Pc(String grantType, String appId, String appSecret, String code, String redirectUrl) {
//		return MessageFormat.format(ApiUrls.PC_ACCESS_TOKEN, grantType, appId, appSecret, code, redirectUrl);
//	}
//	
//	public static String getAccessToken4Wap(String grantType, String appId, String appSecret, String code, String redirectUrl) {
//		return MessageFormat.format(ApiUrls.WAP_ACCESS_TOKEN, grantType, appId, appSecret, code, redirectUrl);
//	}
//	
	public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
		String result = "callback( {\"client_id\":\"YOUR_APPID\",\"openid\":\"YOUR_OPENID\"} )";
		int first = result.indexOf("{");
		int last = result.lastIndexOf("}");
		result = result.substring(first, last + 1);
		System.out.println(result);
		OpenId openId = JsonUtil.json2Bean(result, OpenId.class);
		System.out.println(openId);
	}
	
	
	

}
